local S = minetest.get_translator("basic_tools")
--helper functions
do_nothing = function() end

local function wear_out(itemstack, user)
	local wear = itemstack:get_wear()
	wear = wear + 0x100 --255 uses
	
	--numbers with prefix 0x are in hexadecimal if you didn't know
	if wear < 0xFFFF
	then
		itemstack:set_wear(wear)
		return itemstack
	else
		minetest.sound_play(generic_media.tool_breaks_default(), {object = user})
		return ItemStack(itemstack:get_name() .. "_broken")
	end
end

local function repair(itemstack)
	local wear = itemstack:get_wear()
	wear = wear - 0x4000
	if wear <= 0
	then
		itemstack:set_wear(0)
	else
		itemstack:set_wear(wear)
	end
	return ItemStack(nil)
end



local broken_color = "#FF9090"

local repair_helpstr = "\n" .. S("Use an anvil to repair")


--shovel
------------------------------------------------------------------------
minetest.register_craftitem("basic_tools:shovel_broken",
	{
		description = minetest.colorize(broken_color,
			S("Broken Shovel") .. repair_helpstr),
		inventory_image = "basic_tools_shovel_broken.png",
		groups = {shovel = 1, broken_tool = 1},
		stack_max = 1,
		wield_scale = {x = 2, y = 2, z = 1},
		tool_capabilities =
		{
			full_punch_interval = 2,
			max_drop_level = 3,
			groupcaps =
			{
				crumbly = 
				{
					maxlevel = 3,
					times = {4 , 3.5, 3},
				}
			},
			damage_groups =
			{
				fleshy = 1
			}
		},
		on_drop = do_nothing,
		
	})

minetest.register_tool("basic_tools:shovel",
	{
		description = S("Shovel"),
		inventory_image = "basic_tools_shovel.png",
		groups = {shovel = 1, tool = 1},
		stack_max = 1,
		wield_scale = {x = 2, y = 2, z = 1},
		tool_capabilities =
		{
			full_punch_interval = 2,
			max_drop_level = 3,
			groupcaps =
			{
				crumbly = 
				{
					maxlevel = 3,
					times = {1.5 , 1, 0.4},
				}
			},
			damage_groups =
			{
				fleshy = 1
			}
		},
		after_use = wear_out,
		on_drop = do_nothing,
	})

minetest.register_alias("shovel", "basic_tools:shovel")
minetest.register_alias("broken_shovel", "basic_tools:shovel_broken")
give_initial_stuff.add("shovel")
repairing_anvils.register_recipe("broken_shovel", "shovel 1 65500")
repairing_anvils.register_recipe("shovel", repair)



--pickaxe
------------------------------------------------------------------------
minetest.register_craftitem("basic_tools:pickaxe_broken",
	{
		description = minetest.colorize(broken_color,
			S("Broken Pickaxe") .. repair_helpstr),
		inventory_image = "basic_tools_pickaxe_broken.png",
		groups = {shovel = 1, broken_tool = 1},
		stack_max = 1,
		wield_scale = {x = 2, y = 2, z = 1},
		tool_capabilities =
		{
			full_punch_interval = 2,
			max_drop_level = 3,
			groupcaps =
			{
				cracky = 
				{
					maxlevel = 3,
					times = {5 , 4, 3.5},
				}
			},
			damage_groups =
			{
				fleshy = 1
			}
		},
		on_drop = do_nothing,
	})

minetest.register_tool("basic_tools:pickaxe",
	{
		description = S("Pickaxe"),
		inventory_image = "basic_tools_pickaxe.png",
		groups = {pickaxe = 1, tool = 1},
		stack_max = 1,
		wield_scale = {x = 2, y = 2, z = 1},
		tool_capabilities =
		{
			full_punch_interval = 2,
			max_drop_level = 3,
			groupcaps =
			{
				cracky = 
				{
					maxlevel = 3,
					times = {3 , 2, 1.4},
				}
			},
			damage_groups =
			{
				fleshy = 1
			}
		},
		after_use = wear_out,
		on_drop = do_nothing,
	})
minetest.register_alias("pickaxe", "basic_tools:pickaxe")
minetest.register_alias("broken_pickaxe", "basic_tools:pickaxe_broken")
give_initial_stuff.add("pickaxe")
repairing_anvils.register_recipe("broken_pickaxe", "pickaxe 1 65500")
repairing_anvils.register_recipe("pickaxe", repair)

--axe
------------------------------------------------------------------------
minetest.register_craftitem("basic_tools:axe_broken",
	{
		description = minetest.colorize(broken_color,
			S("Broken Axe") .. repair_helpstr),
		inventory_image = "basic_tools_axe_broken.png",
		groups = {shovel = 1, broken_tool = 1},
		stack_max = 1,
		wield_scale = {x = 2, y = 2, z = 1},
		tool_capabilities =
		{
			full_punch_interval = 2,
			max_drop_level = 3,
			groupcaps =
			{
				choppy = 
				{
					maxlevel = 3,
					times = {4 , 3.5, 3},
				}
			},
			damage_groups =
			{
				fleshy = 1
			}
		},
		on_drop = do_nothing,
	})

minetest.register_tool("basic_tools:axe",
	{
		description = S("Axe"),
		inventory_image = "basic_tools_axe.png",
		groups = {shovel = 1, tool = 1},
		stack_max = 1,
		wield_scale = {x = 2, y = 2, z = 1},
		tool_capabilities =
		{
			full_punch_interval = 2,
			max_drop_level = 3,
			groupcaps =
			{
				choppy = 
				{
					maxlevel = 3,
					times = {2 , 1.5, 0.8},
				}
			},
			damage_groups =
			{
				fleshy = 1
			}
		},
		after_use = wear_out,
		on_drop = do_nothing,
	})

minetest.register_alias("axe", "basic_tools:axe")
minetest.register_alias("broken_axe", "basic_tools:axe_broken")
give_initial_stuff.add("axe")
repairing_anvils.register_recipe("broken_axe", "axe 1 65500")
repairing_anvils.register_recipe("axe", repair)
